<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/4 0004
 * Time: 17:45
 */

namespace app\admin\controller;


use think\Db;

class Gear extends Middle
{
    public function GearList(){
        $res = Db::name('gear')
            ->order('type asc')
            ->select();
        if(!$res)return $this->returnSuccess([]);
        foreach ($res as $k=>$v){
            if($v['type'] == 1)$res[$k]['type'] = '现金充值';
            if($v['type'] == 2)$res[$k]['type'] = '积分兑换服务';
            if($v['type'] == 3)$res[$k]['type'] = '积分兑换数字资产';
            if($v['expire'] == 0)$res[$k]['expire'] = '永久';
        }
        return $this->returnSuccess($res);
    }


    public function GearSet(){
        $id = isset($this->data['id']) ? $this->data['id'] : false;
        $set = [
            'type'      =>$this->data['type'],
            'price'     =>$this->data['price'],
            'desc'      =>$this->data['desc'],
            'expire'    =>$this->data['expire'],
            'vip'       =>1,
        ];
        if($this->data['expire'] >=180)$set['vip'] = 2;
        if($id){
            $set['id'] = $id;
            $res = Db::name('gear')->update($set);
        }else{
            $res = Db::name('gear')->insert($set);
        }
        if($res)return $this->returnSuccess(null,'操作成功');
        return $this->returnError($this->Error);
    }


    public function GearDel(){
        $id = isset($this->data['id']) ? $this->data['id'] : false;
        $map = ['id'=>$id];
        $res = Db::name('gear')->where($map)->find();
        if(!$res)return $this->returnError('参数错误');
        $ref = Db::name('gear')->where($map)->delete();
        if($ref)return $this->returnSuccess(null,'操作成功');
        return $this->returnError($this->Error);
    }

    public function GearDetail(){
        $id = isset($this->data['id']) ? $this->data['id'] : false;
        $map = ['id'=>$id];
        $res = Db::name('gear')->where($map)->find();
        if(!$res)return $this->returnError('参数错误');
        return $this->returnSuccess($res);
    }
}